package com.yueshang.neuron.modular.system.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.yueshang.common.entity.ResultResponse;
import com.yueshang.neuron.common.enums.ResultCodeEnum;
import com.yueshang.neuron.core.log.LogManager;
import com.yueshang.neuron.core.log.factory.LogTaskFactory;
import com.yueshang.neuron.core.shiro.ShiroKit;
import com.yueshang.neuron.core.shiro.ShiroUser;
import com.yueshang.neuron.modular.system.api.IMenuService;
import com.yueshang.neuron.modular.system.api.IUserService;
import com.yueshang.neuron.modular.system.vo.req.LoginReq;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.yueshang.framework.support.HttpKit.getIp;

/**
 * 登录控制器
 *
 * @author shililu
 * @Date 2017年1月10日 下午8:25:24
 */
@RestController
public class LoginController extends ApiController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IUserService userService;


    @Value("${yueshang.kaptcha-open}")
    private boolean kaptchaOpen;

    /**
     * 用户登录
     */
    @PostMapping(value = "/login")
    public ResultResponse login(@Valid LoginReq loginReq) {

//        if (result.hasErrors()) {
//            for (ObjectError er : result.getAllErrors())
//                return ResultResponse.error(er.getDefaultMessage());
//        }

        //验证验证码是否正确
        //todo

        Subject currentUser = ShiroKit.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginReq.getAccount(), loginReq.getPassword().toCharArray());

        if ("on".equals(loginReq.getRemember())) {
            token.setRememberMe(true);
        } else {
            token.setRememberMe(false);
        }

        currentUser.login(token);
        ShiroUser shiroUser = ShiroKit.getUser();
        LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), getIp()));

        return ResultResponse.ok(ResultCodeEnum.OPERATION_SUCCESS.getCode(),"登录成功");
    }

    // 没有登录
    @RequestMapping(value = "/unauthorized", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT})
    public Object unauthorized() throws Exception {
        return ResultResponse.error(ResultCodeEnum.UNAUTH_ERRO.getCode(),"未登录");

    }

    // 没有权限
    @RequestMapping(value = "/forbidden", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT})
    public Object forbidden() {
        return ResultResponse.error(ResultCodeEnum.UNAUTH_ERRO.getCode(),"无权限");
    }
    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ResultResponse logOut() {
        LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), getIp()));
        ShiroKit.getSubject().logout();
        return ResultResponse.ok(ResultCodeEnum.OPERATION_SUCCESS.getCode(),"退出成功");
    }
}
